package com.hzdl.apple.controller.homeCotroll;

import java.io.IOException;

import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.hzdl.apple.bean.User;
import com.hzdl.apple.service.UserService;
import com.hzdl.apple.utils.HttpUtils;




@WebServlet("/user/*")
public class UserController extends HttpServlet{
private UserService service;
	
	@Override
	public void init(ServletConfig config) throws ServletException {
		super.init(config);
		service = new UserService();
		getServletContext().setAttribute("path", getServletContext().getContextPath());
		
	}
	
	@Override
		protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
			String baseUri=req.getRequestURI();
			 
			if (baseUri.contains("/user_login")) {
				String loginId = req.getParameter("loginId");
				String loginPsw = req.getParameter("loginPsw");
				User user=service.login(loginId, loginPsw);
				if (user == null) {
					req.setAttribute("errMsg", "用户名或密码错误！");
					//重新登录
					req.getRequestDispatcher("/home/login").forward(req, resp);
				}else{//登录成功！
					req.getSession().setAttribute("user", user);
					req.getRequestDispatcher("/home/index").forward(req, resp);
				}
			}else if(baseUri.contains("/user_regist")){
				String loginId=req.getParameter("loginId");
				String loginPsw=req.getParameter("loginPsw");
				
				String uName=req.getParameter("uName");
				String uAddress=req.getParameter("uAddress");
				String uPhoneNum=req.getParameter("uPhoneNum");
				String ftg=service.register(uName, loginId, loginPsw, uAddress, uPhoneNum);
				
				if (ftg.equals("该用户已存在！")) {
					
					req.setAttribute("errorMag", "用户名或已存在，请重新注册，");
					HttpUtils.forward("home/user-regist.jsp", req, resp);
				}else if(ftg.equals("注册成功！")){
					//req.setAttribute("errorMag1", "恭喜您！注册成功！");
					
					req.getRequestDispatcher("/home/index.jsp").forward(req, resp);
				}else if (ftg.equals("注册失败！")) {
					
					req.setAttribute("errorMag2", "您输入的信息有误，请重新注册，");
					HttpUtils.forward("home/user-regist.jsp", req, resp);
				}
				
			}else if(baseUri.contains("/user_psw")){
				String oldPassword=req.getParameter("oldPassword");
				String newPassword=req.getParameter("newPassword");
				String reNewPassword=req.getParameter("reNewPassword");
				HttpSession session=req.getSession();
				User u=(User)session.getAttribute("user");
				if(!u.getULoginPsw().equals(oldPassword)){
					req.setAttribute("message", "您输入的原密码不正确！");
					req.getRequestDispatcher("/WEB-INF/jsp/home/user-psw.jsp").forward(req, resp);
					}else if(newPassword.equals(reNewPassword)){
						User user=new User();
					user.setULoginPsw(reNewPassword);
					user.setUID(u.getUID());
					service.changePsw(user, reNewPassword);
					req.setAttribute("Msg", "密码修改成功！");
					//HttpUtils.forward("home/index.jsp", req, resp);
					req.getRequestDispatcher("/WEB-INF/jsp/home/user-psw.jsp").forward(req, resp);
					}else{
					req.setAttribute("message", "您输入的两次新密码不相同！");
					//req.getRequestDispatcher("/WEB-INF/jsp/home/user-psw.jsp").forward(req, resp);
					HttpUtils.forward("home/user-psw.jsp", req, resp);
					}
				//req.getRequestDispatcher("/WEB-INF/jsp/home/user-psw.jsp").forward(req, resp);
			}else if(baseUri.contains("/user_msg")){
				String uMessage=req.getParameter("UMessage");
				String uName=req.getParameter("uName");
				service.leaveMessage(uName, uMessage);
				req.setAttribute("msg", "留言成功！");
				req.getRequestDispatcher("/WEB-INF/jsp/home/user-msg.jsp").forward(req, resp);
				
			}else if(baseUri.contains("/user_info")){
				String UName=req.getParameter("UName");
				
				User user=service.userInfo(UName);
				req.setAttribute("user", user);
			}else if(baseUri.contains("/user_find")){
				String uname = req.getParameter("uname");
				String uphone = req.getParameter("uphone");
				User user=service.findPsw(uname, uphone);
				if (user == null) {
					req.setAttribute("errMsg", "用户名或电话错误！");
					//重新登录
					req.getRequestDispatcher("/home/find").forward(req, resp);
				}else{//修改成功！
					req.getSession().setAttribute("user", user);
					req.setAttribute("findMsg", user.getULoginPsw());
					req.getRequestDispatcher("/WEB-INF/jsp/home/user-find.jsp").forward(req, resp);
				}
			}
			
			
			
			
		}
	
	



	@Override
		protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
			
			super.doPost(req, resp);
		}
}
